package com.crud.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;


public class MyFilter implements Filter {

    private static final Logger logger = Logger.getLogger(MyFilter.class);

    public void destroy() {
        System.out.println("销毁过滤器方法");
        logger.info("销毁过滤器方法");
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        long t1 = System.nanoTime();
        chain.doFilter(request, response);
        long t2 = System.nanoTime();
        System.out.println("***request (" + req.getRequestURI() + ") finished with time(ms): " + (t2 - t1));

    }

    public void init(FilterConfig arg0) throws ServletException {
        System.out.println("初始化过滤器的方法");
        logger.info("初始化过滤器的方法");
    }
}